package com.jqbd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqbd.common.MyJSON;
import com.jqbd.mapper.UserMapper;
import com.jqbd.pojo.User;
import com.jqbd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Enzo Cotter on 2022/11/10.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public MyJSON<User> findAll(Map<String, Object> map) {
        int page = Integer.parseInt(map.get("page") + "");
        int limit = Integer.parseInt(map.get("limit") + "");
        System.out.println(page+"     "+limit);
        PageHelper.startPage(page, limit);
        List<User> userList = userMapper.findAll(map);
        for (User user : userList) {
            System.out.println(user);
        }
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        MyJSON<User> myJSON = new MyJSON<>();
        myJSON.setMsg("用户信息");
        myJSON.setCode(0);
        myJSON.setData(pageInfo.getList());
        myJSON.setCount(pageInfo.getTotal());
        return myJSON;
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public MyJSON<String> selectnickname() {
        List<String> selecttnickname = userMapper.selecttnickname();
        MyJSON<String> myJSON = new MyJSON<>();
        myJSON.setMsg("用户信息");
        myJSON.setCode(0);
        myJSON.setData(selecttnickname);
        myJSON.setCount((long) selecttnickname.size());
        return myJSON;
    }
}
